<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;

use Exception;

class OrderItem extends BaseModel
{
    public $timestamps = false;
    public static function AddItem($orderObject,$input,$user)
    {
        $carts_id = json_decode($input['cart_id'],true);
        $carts = Cart::whereIn('cart_items.id',$carts_id)
            ->join('products_sku','cart_items.product_sku_id', '=', 'products_sku.id')
            ->get()
            ->toArray();
        $amount = 0;
        $amount_score = 0;
        foreach ($carts as $key=>$val) {
            $amount += $val['amount'] * $val['price'];
            $amount_score += self::itemCalcScore($val, $input['buy_type']);
        }
        if($user['score'] < $amount_score) {
            throw new Exception('积分不足');
        }
        $orderObject->total_amount = $amount;
        $orderObject->score_amount = $amount_score;
        $orderObject->real_pay_money = $amount - self::transScoreToMoney($amount_score);
        if (!$orderObject->save()) {
            throw new Exception('订单保存失败1');
        }
        foreach ($carts as $val) {
            $orderItem = new self();
            $orderItem->order_id = $orderObject->id;
            $orderItem->product_id = $val['product_id'];
            $sku = ProductSku::getSkuById($val['product_sku_id']);
            if ($sku) {
                $orderItem->sku_name = $sku['name'];
            }
            $orderItem->spec_item = ProductSpec::getSpecItem($val['product_id']);
            $orderItem->amount = $val['amount'];
            $orderItem->price = $val['price'];
            $orderItem->vip_price = $val['vip_price'];
            $orderItem->vip_score = $val['vip_score'];
            $orderItem->save();
        }
        //删除购物车
        foreach ($carts_id as $val) {
            $cart = Cart::find($val);
            if ($cart) {
                $cart->delete();
            }
        }
       //减库存
        foreach ($carts as $val) {
            $prodcutSku = ProductSku::find($val['product_sku_id']);
            if ($prodcutSku) {
                $prodcutSku->decrement('stock', $val['amount']);
            }
        }
    }

    public static function itemCalcScore($order,$buy_type)
    {
        return $buy_type == 2 ? $order['amount'] * $order['vip_score'] : 0;
    }

    public static function transScoreToMoney($score)
    {
        return $score;
    }

    public static function getOrderItemByOrderId($order_id)
    {
        $order_item = OrderItem::where('order_id', $order_id)
            ->select('order_items.*','title','image')
            ->join('products','order_items.product_id', '=', 'products.id')
            ->get()
            ->toArray();
        foreach ($order_item as $val) {
            $val['image'] = self::jsonDecodeImg($val['image']);
        }
        return $order_item;
    }

    public static function getCommit($product_id)
    {
        $where = [
            ['product_id', '=', $product_id],
            ['is_review', '=', 1],
        ];
        $item = self::where($where)
            ->select('id','order_id','rating','is_review','review','replay','reviewed_at')
            ->paginate(10)
            ->orderBy('id', 'desc')
            ->toArray();
        foreach($item['data'] as $key=>&$val){
            $order = Order::find($val['order_id']);
            if ($order) {
                $user = User::find($order['user_id']);
                if ($user) {
                    $val['user'] = $user->toArray();
                }
            }
        }
        return $item;
    }
}